// pages/local/local.js
var app = getApp();
var api = require("../../conf/api")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    phone:"16676051342",
    code:"",
    attr:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindPhoneInput(e){
    this.setData({phone:e.detail.value})
  },
  bindCodeInput(e){
    this.setData({code:e.detail.value})
  },
  // 登录
  onClickSubmit(){
      let that = this;
      if(!this.data.attr){
        wx.getUserProfile({
          desc:"登录",
          success: function(attr) {
            wx.login({
              success(result){
                that.data.attr = attr
                wx.request({
                  url: api.loginURL,
                  method:'POST',
                  data:{phone:that.data.phone,code:that.data.code,nickname:attr.userInfo.nickName,avatar:attr.userInfo.avatarUrl,wx_code:result.code},
                  success(res){
                    if(res.data.status){
                      app.initUserInfo(res.data.data,attr.userInfo)
                      wx.navigateBack({});
                      // 跳转回上一个页面
                      // let pages = getCurrentPages();
                      // let prevPage = pages[pages.length-2];
                     
                    }else{
                      wx.showToast({
                        title:res.data.message,
                        icon:'none'
                      })
                    }
              }
            })
            
            
              },
              fail(err){
                console.log(err)
              }
            })
          
          }
        })
      }else{
        wx.login({
          success(result){
            wx.request({
              url: 'http://127.0.0.1:8000/api/login/',
              method:'POST',
              data:{phone:that.data.phone,code:that.data.code,nickname:that.data.attr.userInfo.nickName,avatar:that.data.attr.userInfo.avatarUrl,wx_code:result.code},
              success(res){
                if(res.data.status){
                  console.log(that.data.attr.userInfo)
                  app.initUserInfo(res.data.data,that.data.attr.userInfo)
                  wx.navigateBack({});
                  // 跳转回上一个页面
                  // let pages = getCurrentPages();
                  // let prevPage = pages[pages.length-2];
                 
                }else{
                  wx.showToast({
                    title:res.data.message,
                    icon:'none'
                  })
                }
              },
            })
          }
        })
       
      }
     
  },
  // 验证码
  onClickCheckCode(){
    var res=/^(1[3|4|5|6|7|8|9])\d{9}$/;
    if(!res.test(this.data.phone)){
      wx.showToast({
        title:"手机号格式错误",
        icon:'none'
      })
      return;
    }
    wx.request({
      url: api.messageURL,
      data:{phone:this.data.phone,code:this.data.code},
      method:"GET",
      success(res){
        if(res.data.status){
          wx.showToast({
            title:"发送成功",
            icon:'none'
          })
          console.log(res.data)
        }else{
          wx.showToast({
            title:res.data.message,
            icon:'none'
          })
        }
      }
    })
    
  }
})